package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @Reference
    private IUserInfoCacheService userInfoCacheService;

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        return userInfoService.checkPhone(phone);
    }


    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone) {
        AjaxResult result = new AjaxResult();

        //1:先检查该手机号是否有验证码存在
        String verifyCode = verifyCodeCacheService.getVerifyCode(phone);

        //2.如果有验证码则直接返回错误信息
        if (verifyCode != null) {
            return result.mark("验证码已发送");
        }

        //3.产生验证码
        verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.err.println("亲,你的验证码是:"+verifyCode+",请在60S内使用 [叩丁狼教育]");
        //4.缓存验证码
        verifyCodeCacheService.setVerifyCode(phone, verifyCode);
        return result;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String verifyCode,
                             String password, String rpassword) {
        return userInfoService.userRegist(phone, nickname, verifyCode, password, rpassword);
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String phone, String password,
                            HttpServletResponse resp, HttpSession session) {
        //1.调用登录的业务方法
        AjaxResult result = userInfoService.userLogin(phone, password);
        //2.如果登录成功,则返回token给客户端(使用cookie来实现)
        if (result.isSuccess()) {
            String token = (String) result.getData();
            Cookie cookie = new Cookie(Consts.USER_INFO_TOKEN, token);
            cookie.setPath("/");
            //3.返回给客户端
            resp.addCookie(cookie);

            //4.把登录成功的对象存入session
            String userInfo = userInfoCacheService.getUserInfo(token);
            session.setAttribute(Consts.USER_INFO, JSON.parseObject(userInfo, UserInfo.class));
        }
        return result;
    }



    @RequestMapping("verifyCodes")
    @PostMapping
    @ResponseBody
    public AjaxResult send(String phone, @LoginUser UserInfo user){
        AjaxResult json =new AjaxResult();
        try {
            if(phone.equals("")){
                return json.mark("输入手机号不能为空");
            }
            if(!user.getPhone().equals(phone)){
                return json.mark("输入手机号错误,不是当前用户的手机号");
            }
            //1:先检查该手机号是否有验证码存在
            String verifyCode = verifyCodeCacheService.getVerifyCode(phone);

            //2.如果有验证码则直接返回错误信息
            if (verifyCode != null) {
                return json.mark("验证码已发送");
            }
            //3.产生验证码
            verifyCode = UUID.randomUUID().toString().substring(0, 4);
            System.err.println("亲,你的验证码是:"+verifyCode+",请在60S内使用 [叩丁狼教育]");
            //4.缓存验证码
            verifyCodeCacheService.setVerifyCode(phone, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            json.mark(e.getMessage());
        }
        return json;
    }
    @RequestMapping("verifyCodes1")
    @PostMapping
    @ResponseBody
    public AjaxResult send(String phoneTo){
        AjaxResult json =new AjaxResult();
        try {
            if(phoneTo.equals("")){
                return json.mark("输入手机号不能为空");
            }
            if(phoneTo.length()!=11){
                return json.mark("输入手机号格式不正确");
            }
            if(!userInfoService.checkPhone(phoneTo)){
                return json.mark("该手机已经注册,请重新输入");
            }
            //1:先检查该手机号是否有验证码存在
            String verifyCode = verifyCodeCacheService.getVerifyCode(phoneTo);

            //2.如果有验证码则直接返回错误信息
            if (verifyCode != null) {
                return json.mark("验证码已发送");
            }
            //3.产生验证码
            verifyCode = UUID.randomUUID().toString().substring(0, 4);
            System.err.println("亲,你的验证码是:"+verifyCode+",请在60S内使用 [叩丁狼教育]");
            //4.缓存验证码
            verifyCodeCacheService.setVerifyCode(phoneTo, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            json.mark(e.getMessage());
        }
        return json;
    }
    @RequiredLogin
    @RequestMapping("phoneTestSubmit")
    @ResponseBody
    public Object submit(@LoginUser UserInfo user,String phoneTest){
        AjaxResult json = new AjaxResult();
        try {
            System.out.println(phoneTest);
            String verifyCode = verifyCodeCacheService.getVerifyCode(user.getPhone());
            if(phoneTest.equals("")){
                return json.mark("验证码不能为空");
            }
            if(!phoneTest.equals(verifyCode)){
                return json.mark("验证码不正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequiredLogin
    @RequestMapping("phoneTestSubmit1")
    @ResponseBody
    public Object phoneTestSubmit1(@LoginUser UserInfo user,String phoneTest/*验证码*/,String getChangeCode1/*手机号*/){
        AjaxResult json = new AjaxResult();
        try {
            if(phoneTest.equals("")){
                return json.mark("验证码不能为空");
            }
            System.out.println(phoneTest);
            //获取新用户的验证码
            String verifyCode = verifyCodeCacheService.getVerifyCode(getChangeCode1);
            if(!phoneTest.equals(verifyCode)){
                return json.mark("新手机验证码错误");
            }
            //修改用户的手机号
            userInfoService.updateUserPhone(user.getId(),getChangeCode1);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark("修改失败");
        }
        return json;
    }
    @RequiredLogin
    @RequestMapping("input")
    @ResponseBody
    public Object input(@LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //3.产生验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.err.println("亲,你的验证码是:"+verifyCode+",请在60S内使用 [叩丁狼教育]");
        //4.缓存验证码
        verifyCodeCacheService.setVerifyCode(user.getPhone(), verifyCode);
        return result;
    }
    @RequiredLogin//添加邮箱
    @RequestMapping("registeredMail")
    @ResponseBody
    public Object registeredMail(@LoginUser UserInfo user,String email) {
        AjaxResult json = new AjaxResult();
        try {
            //1:先检查邮箱是否有验证码存在
            String verifyCode = verifyCodeCacheService.getVerifyCode(email);
            //2.如果有验证码则直接返回错误信息
            if (verifyCode != null) {
                return json.mark("验证码已发送");
            }
            //3.产生验证码
            verifyCode = UUID.randomUUID().toString().substring(0, 4);
            System.err.println("亲,你的邮箱验证码是:" + verifyCode + ",请在60S内使用 [叩丁狼教育]");
            //4.缓存验证码
            verifyCodeCacheService.setVerifyCode(email, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequiredLogin
    @RequestMapping("emailInput")
    @ResponseBody
    public Object emailSave(@LoginUser UserInfo user,String email,String code){
        AjaxResult result = new AjaxResult();
        try {
            String verifyCode = verifyCodeCacheService.getVerifyCode(email);
            String  password = userInfoService.getUserPassword(user.getId());
            user.setEmail(email);
            user.setPassword(password);
            if(!code.equals(verifyCode)){
                result.setSuccess(false);
                result.mark("邮箱验证码错误");
            }
            userInfoService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }
    @RequiredLogin//退出登入
    @RequestMapping("logout")
    public String logout(@LoginUser UserInfo user,HttpSession session){
        session.removeAttribute(Consts.USER_INFO);
        return "";
    }


}
